package com.up.vms.application.base.utils;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

import java.security.Security;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

/**
 * 文件对象
 * 功能说明：AES 加密算法<br>
 * 模块名称：<br>
 * 功能描述：<br>
 * 文件名称: <br>
 * 系统名称：<br>
 * 软件著作权：icelove 版权所有<br>
 * 开发人员：icelove <br>
 * 开发时间：2019/3/4 12:47<br>
 * 系统版本：1.0.0<br>
 */
public class DecodeUtils {
    private static final String ALGORITHM = "AES";
    private static final String ALGORITHM_MODE_PADDING = "AES/ECB/PKCS7Padding";

    /**
     * AES 解密算法
     *
     * @param data
     * @param key
     * @return
     * @throws Exception
     */
    public static String decryptData(String data, String key) throws Exception {
        Security.addProvider(new BouncyCastleProvider());
        byte[] dataBytes = CryptUtils.base64Decode2Byte(data);
        String mymd5 = CryptUtils.md5(key).toLowerCase();
        SecretKeySpec secretKey = new SecretKeySpec(mymd5.getBytes(), ALGORITHM);
        Cipher cipher = Cipher.getInstance(ALGORITHM_MODE_PADDING);
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        return new String(cipher.doFinal(dataBytes), "utf-8");
    }
}
